home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 2 / CU Amiga Magazine's Super CD-ROM 02 (1996)(EMAP Images)(GB)[!][issue 1996-04].iso / magazine / amiga_e / amigae.june.archive / 000042_crash!kirk.safb.af.mil!BWILLS_Wed, 9 Jun 93 20:30:21 PST.msg < prev    next >
Text File  |  1993-08-31  |  4KB  |  145 lines

  1. Received: by bkhouse.cts.com (V1.16/Amiga)
  2.     id AA00000; Wed, 9 Jun 93 20:30:21 PST
  3. Received: from kirk.safb.af.mil by crash.cts.com with smtp
  4.     (Smail3.1.28.1 #15) id m0o3aw4-000229C; Wed, 9 Jun 93 17:56 PDT
  5. Message-Id: <m0o3aw4-000229C@crash.cts.com>
  6. Date: 9 Jun 93 19:53:00 CST
  7. From: "Barry D. Wills" <BWILLS@kirk.safb.af.mil>
  8. To: "amigae" <amigae@bkhouse.cts.com>
  9. Subject: Linked Lists and Lists (2 of 3)
  10.  
  11. ENUM ER_NONE,
  12.      ER_MEM
  13.  
  14.  
  15. RAISE ER_MEM IF List () = NIL,
  16.       ER_MEM IF String () = NIL
  17.  
  18.  
  19. OBJECT listElementType
  20.   name, phoneNumber
  21. ENDOBJECT
  22.  
  23.  
  24. DEF globalListElement : PTR TO listElementType
  25.  
  26.  
  27. PROC newElement (name, phoneNumber)
  28.   DEF el : PTR TO listElementType
  29.   el := New (SIZEOF listElementType)
  30.   el.name := String (10)
  31.   StrCopy (el.name, name, ALL)
  32.   el.phoneNumber := phoneNumber
  33. ENDPROC  el
  34.  
  35.  
  36. PROC print ()
  37.   WriteF ('\s \d\n', globalListElement.name, globalListElement.phoneNumber)
  38. ENDPROC
  39.  
  40.  
  41. PROC tailOfList (theList)
  42.   DEF tail
  43.   tail := theList
  44.   WHILE Next (tail) <> NIL DO tail := Next (tail)
  45. ENDPROC  tail
  46.  
  47.  
  48. PROC appendList (theList, listToAppend)
  49.   VOID Link (tailOfList (theList), listToAppend)
  50. ENDPROC
  51.  
  52.  
  53. PROC printAll (theList)
  54.   WriteF ('\n  Contents of list: \n')
  55.   readAll (theList, `print ())
  56. ENDPROC
  57.  
  58.  
  59. PROC incrementAll (theList)
  60.   writeAll (theList, `globalListElement + 1)
  61. ENDPROC
  62.  
  63.  
  64. PROC readAll (theList, func)
  65.   DEF link
  66.   link := theList
  67.   WHILE link <> NIL
  68.     ForAll ({globalListElement}, link, func)
  69.     link := Next (link)
  70.   ENDWHILE
  71. ENDPROC
  72.  
  73.  
  74. PROC writeAll (theList, func)
  75.   DEF link
  76.   link := theList
  77.   WHILE link <> NIL
  78.     MapList ({globalListElement}, link, link, func)
  79.     link := Next (link)
  80.   ENDWHILE
  81. ENDPROC
  82.  
  83.  
  84. PROC main () HANDLE
  85.   DEF listHead = NIL,
  86.       listCurrent,
  87.       newListElement : PTR TO listElementType,
  88.       name,
  89.       phoneNumber,
  90.       counter
  91.  
  92.   /* Allocate and initialize front of list. */
  93.   listHead := List (1)
  94.   newListElement := newElement ('Beeblebrox', 3333333)
  95.   ListCopy (listHead, [newListElement], ALL)
  96.  
  97.   /* We now have one list element containing a name and a phone number */
  98.   /* which is pointed to by variable listHead.                         */
  99.  
  100.   printAll (listHead)
  101.  
  102.  
  103.   /* Allocate and initialize a second list element, then link it. */
  104.   listCurrent := List (1)
  105.   newListElement := newElement ('Aardvark', 1111111)
  106.   ListCopy (listCurrent, [newListElement], ALL)
  107.   appendList (listHead, listCurrent)
  108.  
  109.   /* We now have two list elements each containing a name and a phone     */
  110.   /* number which is linked to the end of the list pointed to by variable */
  111.   /* listHead.  It can be accessed by using the function Next () (see     */
  112.   /* function readAll () which is called by function printAll ().)        */
  113.  
  114.   printAll (listHead)
  115.  
  116.  
  117.   /* Allocate and initialize a third list element, then link it. */
  118.   listCurrent := List (1)
  119.   newListElement := newElement ('Cookamonga', 2222222)
  120.   ListCopy (listCurrent, [newListElement], ALL)
  121.   appendList (listHead, listCurrent)
  122.  
  123.   /* We now have three list elements each containing a name and a phone */
  124.   /* number which is linked to end of the list pointed to by variable   */
  125.   /* listHead.  It can be accessed by successive calls to the function  */
  126.   /* Next () (see function readAll () which is called by function       */
  127.   /* printAll ().)                                                      */
  128.  
  129.   printAll (listHead)
  130.  
  131.  
  132.   DisposeLink (listHead)  /* WARNING!  listHead will be a NIL pointer    */
  133.                           /* after this function call even though it was */
  134.                           /* not allocated using the List () function.   */
  135.  
  136.   WriteF ('\n\n')
  137.   CleanUp (0)
  138.  
  139. EXCEPT
  140.  
  141.   IF exception = ER_MEM THEN WriteF ('\n\n *** Out of memory.\n\n')
  142.  
  143.   CleanUp (exception)
  144.  
  145. ENDPROC